package test.tool.calculator;

import com.google.common.collect.ImmutableMap;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

/**
 * description: <br>
 * createTime: 2018/10/2911:11 <br>
 *
 * @author zzh
 */
public class CalculatorWithStack {

    private String calString;

    private LinkedList<Double> operandStack = new LinkedList();

    private LinkedList<Operator> operatorStack = new LinkedList();

    private StringBuffer postCalculateString = new StringBuffer("");

    private static final Set<String> OPERATOR_SET = new HashSet<>();

    private static final Map<String, Operator> OPERATOR_MAP;

    static {
        OPERATOR_SET.addAll(Arrays.asList("+-*/%()".split("")));
        OPERATOR_MAP = ImmutableMap.<String, Operator> builder()
                .put("+", Operator.PLUS)
                .put("-", Operator.MINUS)
                .put("*", Operator.MULTIPLE)
                .put("/", Operator.DIVIDE)
                .put("%", Operator.MOD)
                .put("(", Operator.PLUS)
                .put(")", Operator.PLUS)
                .build();
    }

    public CalculatorWithStack(String calString) {
        this.calString = calString;
    }


    public void parse() {
        LinkedList<String> stack = new LinkedList<>();
        int startIndex = 0;
        do {
            Integer nextOperatorIndex = findOperator(startIndex);
            if(nextOperatorIndex == null) {
                //没找到操作符，作校验
            } else {
                String operatorString = calString.substring(nextOperatorIndex, nextOperatorIndex);
            }
        } while (startIndex < calString.length());
    }

    private Integer findOperator(int startIndex) {
        while(startIndex < calString.length()) {
            if(isOperator(calString.charAt(startIndex))) {
                return  startIndex;
            }
            startIndex++;
        }
        return null;
    }

    private boolean isOperator(char c) {
        return OPERATOR_SET.contains(c);
    }

    private boolean isLeftBracket(String str) {
        return "(".equals(str);
    }

    private boolean isRightBracket(String str) {
        return ")".equals(str);
    }

    public Double calculate() {

        return null;
    }


    public String getPostExpression() {

        return null;
    }

    enum Operator{
        PLUS() {
            @Override
            public double operate(double val1, double val2) {
                return val1 + val2;
            }
        }, MINUS() {
            @Override
            public double operate(double val1, double val2) {
                return val1 - val2;
            }
        }, MULTIPLE() {
            @Override
            public double operate(double val1, double val2) {
                return val1 * val2;
            }
        }, DIVIDE() {
            @Override
            public double operate(double val1, double val2) {
                return val1 / val2;
            }
        }, MOD() {
            @Override
            public double operate(double val1, double val2) {
                return val1 % val2;
            }
        };
        public abstract double operate(double val1, double val2);
    }

}
